package org.jeecg.modules.audit.controller;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.audit.entity.ConContentRemark;
import org.jeecg.modules.audit.service.IConContentRemarkService;
import org.jeecg.modules.content.entity.ContentInfo;
import org.jeecg.modules.content.service.IContentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 内容评论
 * @Author: jeecg-boot
 * @Date:   2020-05-02
 * @Version: V1.0
 */
@Slf4j
@Api(tags="内容评论")
@RestController
@RequestMapping("/content/conContentRemark")
public class ConContentRemarkController extends JeecgController<ConContentRemark, IConContentRemarkService> {
	@Autowired
	private IConContentRemarkService conContentRemarkService;

	@Autowired
	private IContentInfoService contentInfoService;
	/**
	 * 分页列表查询
	 *
	 * @param conContentRemark
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "内容评论-分页列表查询")
	@ApiOperation(value="内容评论-分页列表查询", notes="内容评论-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ConContentRemark conContentRemark,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ConContentRemark> queryWrapper = QueryGenerator.initQueryWrapper(conContentRemark, req.getParameterMap()).orderByDesc("create_time");
		Page<ConContentRemark> page = new Page<ConContentRemark>(pageNo, pageSize);
		IPage<ConContentRemark> pageList = conContentRemarkService.page(page, queryWrapper);

		pageList.getRecords().stream().forEach(item->{
			//获取title
			String contentId = item.getContentId();
			QueryWrapper<ContentInfo> contentInfoQueryWrapper = new QueryWrapper<ContentInfo>();
			contentInfoQueryWrapper.eq("id",contentId).last(" limit 1");
			ContentInfo contentInfo = contentInfoService.getOne(contentInfoQueryWrapper);
			item.setTitle(contentInfo.getContentTitle());
		});
		return Result.ok(pageList);
	}

	 /**
	  *
	  * @param ids
	  * @param checkResult
	  * @return
	  */
	 @AutoLog(value = "内容评论-审核")
	 @ApiOperation(value="内容评论-审核", notes="内容评论-审核 checkResult:1 通过 2 未通过")
	 @PutMapping(value = "/check")
	 public Result<?> edit(HttpServletRequest request,@RequestParam(name="ids",required=true) String ids, @RequestParam(name="checkResult",required=true) Integer checkResult) {
		 List<String> idList = Arrays.asList(ids.split(","));
		 String checker = JwtUtil.getUserNameByToken(request);
		 List<ConContentRemark> contentRemarkList = new ArrayList<ConContentRemark>();
		 for(String id : idList){
			 ConContentRemark contentRemark = new ConContentRemark();
			 contentRemark.setId(id);
			 contentRemark.setCheckResult(checkResult);
			 contentRemark.setChecker(checker);
			 contentRemarkList.add(contentRemark);
		 }
		 conContentRemarkService.updateBatchById(contentRemarkList);
		 return Result.ok("审核成功!");
	 }


	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "内容评论-通过id查询")
	@ApiOperation(value="内容评论-通过id查询", notes="内容评论-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ConContentRemark conContentRemark = conContentRemarkService.getById(id);
		return Result.ok(conContentRemark);
	}


	/**
	 * 编辑
	 *
	 * @param conContentRemark
	 * @return
	 */
	@AutoLog(value = "内容评论-编辑")
	@ApiOperation(value="内容评论-编辑", notes="内容评论-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ConContentRemark conContentRemark,HttpServletRequest request) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		UpdateWrapper<ConContentRemark> updateWrapper  =  new UpdateWrapper<>();
		updateWrapper.set("check_result",conContentRemark.getCheckResult());
		updateWrapper.set("checker",loginUser.getUsername());
		updateWrapper.eq("id",conContentRemark.getId());
		conContentRemarkService.update(updateWrapper);
		return Result.ok("编辑成功!");
	}



}
